$(function () {
    var salaryDetail_datagrid = $("#salaryDetail_datagrid");
    var salaryDetail_dialog = $("#salaryDetail_dialog");
    var detailId=null;
    var allData = {};  //保存所有详单
    //工资详单列表初始化
    salaryDetail_datagrid.datagrid({
        fit: true,
        url: "/salaryDetail/list",
        fitColumns: false,
        title:"工资详细",
        pagination: true,
        rownumbers: true,
        striped: true,
        toolbar: "#tb",
        singleSelect: true,
        frozenColumns:[[
            {field: "ck", checkbox: true},  //显示多选框样式
            {field: "id", title: "编号", hidden: true},

            {
                field: 'staff', title: '姓名', width: 100,align:'center',formatter: function (value, row, index) {

                return row.staff.name?row.staff.name:"";
              }
            },
        ]],
        columns: [[

            {
                field: 'staff_duty', title: '职位', width: 100,align:'center', formatter: function (value, row, index) {
                return row.staff.duty.name? row.staff.duty.name:"";
                }
            },            {
                field: 'staff_basePay', title: '基本工资', width: 100,align:'center', formatter: function (value, row, index) {
                return row.staff.personageSalary.basePay?row.staff.personageSalary.basePay:"";
                }
            },
            {
                field: 'staff_postSubsidy', title: '岗位津贴', width: 100,align:'center', formatter: function (value, row, index) {
                return row.staff.personageSalary.postSubsidy?row.staff.personageSalary.postSubsidy:"";
              }
            },
            {
                field: 'staff_skillSubsidy', title: '技术补贴', width: 100,align:'center', formatter: function (value, row, index) {
                return  row.staff.personageSalary.skillSubsidy?row.staff.personageSalary.skillSubsidy:"";
              }
            },

            {field: "bonus", title: "奖金及通讯费",align:'center', width: 160},

            {field: "reward", title: "奖励",align:'center', width: 100},

            {field: "overtimePay", title: "加班",align:'center', width: 100},

            {field: "onDuty", title: "值班",align:'center', width: 100},

            {field: "techCost", title: "讲课费",align:'center', width: 100},

            {field: "annualbonus", title: "年终奖",align:'center', width: 100},

            {field: "fillCost", title: "补足",align:'center', width: 100},

            {field: "withdrawing", title: "扣款",align:'center', width: 100},

            {field: "checkworkdrawing", title: "考勤扣款",align:'center', width: 100,readOnly:true},

            {field: "grosspay", title: "应发工资",align:'center', width: 100,readOnly:true},

            {field: "socialsecurity", title: "社保",align:'center', width: 100},

            {field: "accumulationfund", title: "公积金",align:'center', width: 100},

            {field: "tax", title: "个人税",align:'center', width: 100,readOnly:true},

            {field: "payroll", title: "实发工资",align:'center', width: 100,readOnly:true},

            {
                field: 'staff_remark', title: '备注',align:'center', width: 100, formatter: function (value, row, index) {
                return row.staff.personageSalary.remark?row.staff.personageSalary.remark:"";
              }
            }

        ]]
    })
    //工资详单列表初始化-end

    //编辑或更新对话框
    salaryDetail_dialog.dialog({
        width: 700,
        height: 400,
        buttons: "#bb",
        closed: true
    })


    //响应函数集合对象
    var cmdObj = {
        //工具栏的刷新
        reload: function () {
            salaryDetail_datagrid.datagrid("load");
        },
        //工具栏的编辑
        edit: function () {
            //判断是否选中数据
            var row = salaryDetail_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            detailId = row.id;

            //添加与编辑时将数据清空
            $("#salaryDetail_form").form("clear");
            //部门名字不一样, 需要修改

           /* if (row.dept) {
                row["dept.id"] = row.dept.id;
            }*/

            //数据显: 同名原则
            $("#salaryDetail_form").form("load", row);

            //1:弹框
            salaryDetail_dialog.dialog("open");
            //2:设置标题
            salaryDetail_dialog.dialog("setTitle", "工资详单个人编辑");
        },


        //保存操作
        save: function () {
            $("#salaryDetail_form").form('submit', {
                url: "salaryDetail/update?id="+detailId,
                success: function (data) {
                    data = $.parseJSON(data);

                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);

                    } else {
                        //关闭框
                        salaryDetail_dialog.dialog("close");
                        //刷新列表数据
                        salaryDetail_datagrid.datagrid("load");
                    }
                }
            })
        },


        cancel: function () {
            salaryDetail_dialog.dialog("close");
        },




        //工具栏的高级查询
        query: function query() {
            var attence_month = $("#attence_month").val();
            attence_datagrid.datagrid('load', {
                month: attence_month
            });
        }


    }//end
    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
})



/*$(function () {
    var attence_month = $("#attence_month");
    attence_month.datebox({
        //显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
        onShowPanel: function () {
            //触发click事件弹出月份层
            span.trigger('click');
            if (!tds)
            //延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                setTimeout(function () {
                    tds = p.find('div.calendar-menu-month-inner td');
                    tds.click(function (e) {
                        //禁止冒泡执行easyui给月份绑定的事件
                        e.stopPropagation();
                        //得到年份
                        var year = /\d{4}/.exec(span.html())[0],
                            //月份
                            //之前是这样的month = parseInt($(this).attr('abbr'), 10) + 1;
                            month = parseInt($(this).attr('abbr'), 10);

                        //隐藏日期对象
                        attence_month.datebox('hidePanel')
                        //设置日期的值
                            .datebox('setValue', year + '-' + month);
                    });
                }, 0);
        },
        //配置parser，返回选择的日期
        parser: function (s) {
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        //配置formatter，只返回年月 之前是这样的d.getFullYear() + '-' +(d.getMonth());
        formatter: function (d) {
            var currentMonth = (d.getMonth() + 1);
            var currentMonthStr = currentMonth < 10 ? ('0' + currentMonth) : (currentMonth + '');
            return d.getFullYear() + '-' + currentMonthStr;
        }
    });

    //日期选择对象
    var p = attence_month.datebox('panel'),
        //日期选择对象中月份
        tds = false,
        //显示月份层的触发控件
        span = p.find('span.calendar-text');
    var curr_time = new Date();
    //设置前当月
    attence_month.datebox("setValue", formatterDate(curr_time));

    //格式化日期
    function formatterDate(date) {
        //获取年份
        var y = date.getFullYear();
        //获取月份
        var m = date.getMonth() + 1;
        return y + '-' + m;
    }
});*/
